/*
 * @Author: zzr
 * @Date: 2024-12-10 14:07:15
 * @LastEditors: zzr
 * @Description:
 */
import { defineStore } from "pinia";
import { store } from "@/store";
import Storage from "good-storage";
import { getMenu } from "@/api/menu";

let THEME_KEY = "theme";
export const useUserStore = defineStore("user", {
  state: () => ({
    user: null,
    adminUser: null,
    theme: Storage.get(THEME_KEY) ?? "dark",
    menu: null,
  }),
  getters: {
    getUser(state) {
      return state.user;
    },
    getTheme(state) {
      return state.theme;
    },
    getMenu(state) {
      return state.menu;
    },
    getUserInfo(state) {
      if (state.user) {
        return state.user?.user;
      } else {
        return {};
      }
    },
    getAdminUser(state) {
      return state.adminUser;
    },
  },
  actions: {
    setTheme(theme) {
      this.theme = theme;
      Storage.set(THEME_KEY, theme);
    },
    async setUser(user) {
      this.user = user;
      getMenu().then((res) => {
        this.menu = res;
      });
    },
    setMenu(menu) {
      this.menu = menu;
    },
    setAdminUser(user) {
      this.adminUser = user;
    },
    removeUser() {
      // this.user = null;
      // this.menu = null;
      this.$reset();
      if (document.querySelectorAll(".el-message").length) {
        document.querySelectorAll(".el-message").forEach((item) => {
          item.remove();
        });
      }
      getMenu().then((res) => {
        this.menu = res;
      });
    },
    removeAdmin() {
      this.adminUser = null;
    },
  },
  persist:{
    paths:['theme']
  },
});
export const useUserStoreWithOut = () => {
  return useUserStore(store);
};
